package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.entity.ProductCategory;
import eteam.aps.model.vo.ProductCategoryVo;
import eteam.aps.service.ProductCategoryService;
import eteam.aps.service.ProductInfoService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 产品分类控制器
 *
 * @author WMT
 */
@Api(tags = "产品分类模块")
@RestController
public class ProductCategoryController implements IProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductInfoService equipInfoService;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Override
    @ApiOperation("新增产品分类")
    @PostMapping("/ec/insert")
    public R<Integer> insert(@RequestBody ProductCategory productCategory) throws Exception, AuthException {

        VerifyTokenDto user = loginUserRedisService.getUser();
        productCategory.setfCreateUserId(user.getFuserid());
        productCategory.setfBookCode(user.getFgrouporgid());

        if (Helper.isEmpty(productCategory.getfEcCode()) || Helper.isEmpty(productCategory.getfEcName())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类编号或产品不能为空");
        }
        if (productCategoryService.selectCountEcName(productCategory.getfEcName()) > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类名称已存在");
        }
        if (productCategoryService.selectCountEcCode(productCategory.getfEcCode()) > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类编号已存在");
        }
        return R.success(productCategoryService.insert(productCategory));
    }

    @Override
    @ApiOperation("删除产品分类")
    @PostMapping("/ec/delete/{fId}")
    public R<Integer> delete(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类fId不能为空");
        }
        if (equipInfoService.selectCountAllByFEcId(fId) > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"该产品分类下有产品数据");
        }
        ProductCategory ecDb = productCategoryService.selectFEcByFId(fId);
        if (ecDb == null){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类fId不存在");
        }
        if (productCategoryService.selectFState(fId) == ParentEntity.FSTATE_DELETED){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类已被删除");
        }
        return R.success(productCategoryService.delete(fId));
    }

    @Override
    @ApiOperation("修改产品分类")
    @PostMapping("/ec/update")
    public R<Integer> update(@RequestBody ProductCategory productCategory) throws Exception, AuthException {

        VerifyTokenDto user = loginUserRedisService.getUser();
        productCategory.setfLastModifyUserId(user.getFuserid());

        if (Helper.isEmpty(productCategory.getfId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类fId不能为空");
        }
        if (Helper.isEmpty(productCategory.getfEcCode()) || Helper.isEmpty(productCategory.getfEcName())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类编号或名称不能为空");
        }
        ProductCategory ecDb = productCategoryService.selectFEcByFId(productCategory.getfId());
        if (!ecDb.getfEcCode().equals(productCategory.getfEcCode())){
            if (productCategoryService.selectCountEcCode(productCategory.getfEcCode()) > 0){
                throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类编号已存在");
            }
        }
        if (!ecDb.getfEcName().equals(productCategory.getfEcName())){
            if (productCategoryService.selectCountEcName(productCategory.getfEcName()) > 0){
                throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类名称已存在");
            }
        }
        return R.success(productCategoryService.update(productCategory));
    }

    @Override
    @ApiOperation("查询产品分类详情")
    @PostMapping("/ec/selectDetails/{fId}")
    public R<ProductCategoryVo> selectDetails(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类fId不能为空");
        }
        ProductCategory ecDb = productCategoryService.selectFEcByFId(fId);
        if (ecDb == null){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"产品分类fId不存在");
        }
        return R.success(productCategoryService.selectDetails(fId));
    }

    @Override
    @ApiOperation("分页查询产品分类")
    @PostMapping("/ec/selectAll")
    public R<JsonPage<ProductCategoryVo>> selectAll(
            @RequestParam(value = "fEcCode",required = false) String fEcCode,
            @RequestParam(value = "fEcName",required = false) String fEcName,
            @RequestParam("page") Integer page,
            @RequestParam("pageSize") Integer pageSize
    ) throws Exception, AuthException {
        Map<String, Object> map = new HashMap<String, Object>(4);
        if (Helper.isNotEmpty(fEcCode)) {
            map.put("fEcCode", Helper.likeSql(fEcCode));
        }
        if (Helper.isNotEmpty(fEcName)) {
            map.put("fEcName", Helper.likeSql(fEcName));
        }
        return R.success(productCategoryService.selectAll(map, Helper.valuePositive(page, 1),
                Helper.valuePositive(pageSize, 10)));
    }
}
